
var rowIdRet = null;


$(function(e){
	dataInit();
});

function dataInit(){
	eventInt();
	$('#messageTable').bootstrapTable({
		url: path+'0193603971d842dfa1526c6cfb528ecf', method: 'post', striped: true, cache: false, pagination: true,
        contentType: "application/x-www-form-urlencoded", sidePagination: "server", pageSize: 10, uniqueId: "id", detailView: false,
        queryParams: function (params) {
        	return { limit: params.limit, offset: params.offset,searchParams:$("#searchParams").val()};
        },//传递参数（*）
        columns:[{field: 'Number', title: '序号', align: 'center', width: '50', formatter: function (value, row, index) {
        			return index+1;
        		}}, 
        		{field: 'name', title: '姓名', align: 'center', width: '150', formatter: function (value, row, index) {
        			if(isNull(value)){
	            		value = '-';
	            	}
        			return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'sex', title: '性别', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'age', title: '年龄', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'phone', title: '联系方式', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'qq', title: 'QQ', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'wechat', title: '微信', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'type', title: '维修人员类别', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'post', title: '职务', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},

	            {field: 'operate', title: '操作', align: 'center', width: '210', events: {
	            	 'click .RoleOfA': function (e, value, row, index) {
	            		 	editServiceMan(row.id);
		                },
		                'click .RoleOfB': function (e, value, row, index) {
		                	qiao.bs.confirm("确定要删除此项吗？",function(){
			                	deleteServiceMan(row.id);
			                },function(){});
		                },
	            },
	            formatter: function (value, row, index) {
	            	return [
						'<button type="button" class="RoleOfA btn btn-default  btn-sm" style="margin-right:15px;">编辑</button>',
						'<button type="button" class="RoleOfB btn btn-default  btn-sm" style="margin-right:15px;">删除</button>',

	        	        ].join('');
	            }
        }],
        onLoadSuccess: function(data){  //加载成功时执行
        	if(data.returnCode!=0){
        		qiao.bs.msg({msg:data.returnMessage,type:'danger'});
        	}
        }
    });
	
	//修改
	$('#updateServiceManForm').bootstrapValidator({
        fields: {
        	updatename: {validators: {notEmpty: {message: '姓名不能为空'}}},
        	updatesex: {validators: {notEmpty: {message: '性别不能为空'}}},
        	updateage: {validators: {notEmpty: {message: '年龄不能为空'}}},
        	updatephone: {validators: {notEmpty: {message: '联系方式不能为空'},
	    		stringLength: {
		    	    min: 11,
		    	    max: 11,
		    	    message: '请输入11位手机号码'
		    	   },
		    	   regexp: {
		    	    regexp: /^1[3|5|8]{1}[0-9]{9}$/,
		    	    message: '请输入正确的手机号码'
		    	   }}},
        	updateqq: {validators: {notEmpty: {message: 'QQ不能为空'}}},
        	updatewechat: {validators: {notEmpty: {message: '微信不能为空'}}},
        	updatetype: {validators: {notEmpty: {message: '维修人员类别不能为空'}}},
        	updatepost: {validators: {notEmpty: {message: '职务不能为空'}}},
        },
        submitHandler: function (validator, form, submitButton) {
        	var params = null;
    			params = {
					rowId:rowIdRet,
					name:$("#updatename").val(),
					sex:$("#updatesex").val(),
					age:$("#updateage").val(),
					phone:$("#updatephone").val(),
					qq:$("#updateqq").val(),
					wechat:$("#updatewechat").val(),
					type:$("#updatetype").val(),
					post:$("#updatepost").val(),
    			};
    		console.log(params);
    		AjaxPostUtil.request({url:path+"a53adb4f098d4222b1a87d49c923ae09",params:params,type:'json',callback:function(json){
    			if(json.returnCode==0){
    				qiao.bs.msg({msg:"保存成功",type:'success'});
    				$("#updateServiceManMation").modal("hide");
    				$('#messageTable').bootstrapTable('refresh',{url:path+'0193603971d842dfa1526c6cfb528ecf'});
    			}else{
    				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
    			}
    		}});
    		return false;
        }
    });
	
	
	
	
	//增加维修人员
	$('#addServiceManForm').bootstrapValidator({
	    fields: {
	    	name: {validators: {notEmpty: {message: '姓名不能为空'}}},
	    	sex: {validators: {notEmpty: {message: '性别不能为空'}}},
	    	age: {validators: {notEmpty: {message: '年龄不能为空'}}},
	    	phone: {validators: {notEmpty: {message: '联系方式不能为空'},
	    		stringLength: {
	    	    min: 11,
	    	    max: 11,
	    	    message: '请输入11位手机号码'
	    	   },
	    	   regexp: {
	    	    regexp: /^1[3|5|8]{1}[0-9]{9}$/,
	    	    message: '请输入正确的手机号码'
	    	   }}},
	    	qq: {validators: {notEmpty: {message: 'QQ不能为空'}}},
	    	wechat: {validators: {notEmpty: {message: '微信不能为空'}}},
	    	type: {validators: {notEmpty: {message: '维修人员类别不能为空'}}},
	    	post: {validators: {notEmpty: {message: '职务不能为空'}}},
	    },
	    submitHandler: function (validator, form, submitButton) {
	    	var params = {
	    		name:$("#name").val(),
	    		sex:$("#sex").val(),
	    		age:$("#age").val(),
	    		phone:$("#phone").val(),
	    		qq:$("#qq").val(),
	    		wechat:$("#wechat").val(),
	    		type:$("#type").val(),
	    		post:$("#post").val(),
	    		//createId:$("#createId").val(),
				
	    	};
	    	
	    	console.log(params);
	    	AjaxPostUtil.request({url:path+"d231b30cb07d4ea9bdc609e610ebf517",params:params,type:'json',callback:function(json){
				if(json.returnCode==0){
					qiao.bs.msg({msg:"维修人员添加成功",type:'success'});
					$("#addServiceManMation").modal("hide");
					$('#messageTable').bootstrapTable('refresh',{url:path+'0193603971d842dfa1526c6cfb528ecf'});
				}else{
					qiao.bs.msg({msg:json.returnMessage,type:'danger'});
				}
			}});
			return false;
	    }
	});
}
	//删除
function deleteServiceMan(rowId){
	AjaxPostUtil.request({url:path+"575defd2bc664d7c81fc2000a006fc1b",params:{rowId:rowId},type:'json',callback:function(json){
		if(json.returnCode==0){
			qiao.bs.msg({msg:"删除成功",type:'success'});
			$('#messageTable').bootstrapTable('refresh',{url:path+'0193603971d842dfa1526c6cfb528ecf'});
		}else{
			qiao.bs.msg({msg:json.returnMessage,type:'danger'});
		}
	}});
}



function editServiceMan(rowId){
	AjaxPostUtil.request({url:path+"a37e937e93914cc1ba263d6e21b804c8",params:{rowId:rowId},type:'json',callback:function(json){
		console.log(json);
		if(json.returnCode==0){
			    rowIdRet = json.bean.id;
				$("#updatename").val(json.bean.name);
				$("#updatesex").val(json.bean.sex);
				
				$("#updateage").val(json.bean.age);
				$("#updatephone").val(json.bean.phone);
				$("#updateqq").val(json.bean.qq);
				$("#updatewechat").val(json.bean.wechat);
				$("#updatetype").val(json.bean.type);
				
				
				$("#updatepost").val(json.bean.post);
				$("#updateServiceManMation").modal("show");
		}else{
				qiao.bs.msg({msg:json.returnMessage,type:'danger'});
			}
	}});

		
}


function eventInt(){
	
	$('body').on('click', '#addServiceMan', function(e){
		$("input").val("");
		$("#addServiceManMation").modal("show");
	});	
	
	//回车模糊搜索
	$('body').keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
        	var params = {
        			limit : 10,
        			offset : 0,
        			searchParams : $("#searchParams").val()
        	}
        	AjaxPostUtil.request({url : path+"0193603971d842dfa1526c6cfb528ecf", params : params, type : 'json', callback : function(json){
        		if(json.returnCode == 0){
        			console.log(json);
        			$('#messageTable').bootstrapTable('refresh',{url : path+'0193603971d842dfa1526c6cfb528ecf'});
        		}else{
        			qiao.bs.msg({msg : '参数错误.', type : 'danger'});
        		}
        	}});
        }
	});
}

